package com.fadadianqi.logistics.admin.ticket.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.logistics.admin.ticket.model.Prize;
import com.fadadianqi.logistics.admin.ticket.service.LotteryDrawService;
import com.fadadianqi.logistics.admin.ticket.service.PrizeService;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.fadadianqi.logistics.sell.model.Bill;
import com.fadadianqi.logistics.sell.service.BillService;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
public class LotteryDrawController {


    @RequestMapping("/a/ticket/ajax/billInfo")
    @ResponseBody
    public ResponseEntity<String> getBillInfo(HttpServletRequest request
            , HttpServletResponse response
            , ModelMap modelMap) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        String billId = ParamUtils.getString(request, "bid", null);
        Bill bill = billService.getById(billId);
        BillInfo info = new BillInfo();
        AjaxResult rs = null;
        if (null != bill) {
            Customer customer = customerService.getById(bill.getCustomerId());
            info.setBill(bill);
            info.setCustomer(customer);
            info.setDrawNumber(lotteryDrawService.getDrawNumber(bill));
            info.setWinnerCount(lotteryDrawService.countWinnerByBillId(bill));
            rs = new AjaxResult(info, Boolean.TRUE, null);
        } else {
            rs = new AjaxResult(info, Boolean.FALSE, null);
        }
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }


    /**
     * 用户抽奖页面
     *
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping("/a/ticket/lotterydraw")
    public String showForm(HttpServletRequest request, ModelMap modelMap) {


        Pagination pagination = new Pagination(request, 100);
        List<Prize> prizeItems = prizeService.findAllWithNotDraw(pagination);


        modelMap.put("prizeItems", prizeItems);

        return "/admin/ticket/lotterydraw";
    }

    @Autowired
    private LotteryDrawService lotteryDrawService;

    @Autowired
    private PrizeService prizeService;

    @Autowired
    private BillService billService;

    @Autowired
    private CustomerService customerService;

    public class BillInfo {
        private Bill bill;
        private Customer customer;
        private int drawNumber;
        private int winnerCount;

        public Bill getBill() {
            return bill;
        }

        public void setBill(Bill bill) {
            this.bill = bill;
        }

        public Customer getCustomer() {
            return customer;
        }

        public void setCustomer(Customer customer) {
            this.customer = customer;
        }

        public int getDrawNumber() {
            return drawNumber;
        }

        public void setDrawNumber(int drawNumber) {
            this.drawNumber = drawNumber;
        }

        public int getWinnerCount() {
            return winnerCount;
        }

        public void setWinnerCount(int winnerCount) {
            this.winnerCount = winnerCount;
        }
    }
}
